package com.bnz.shop.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.bnz.common.entity.R;
import com.bnz.shop.entity.ItemCatEntity;
import com.bnz.shop.entity.group.Goods;
import com.bnz.shop.service.ItemCatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.bnz.shop.entity.GoodsEntity;
import com.bnz.shop.service.GoodsService;
import com.bnz.common.utils.PageUtils;




/**
 * 商品
 *
 * @author Haziy
 * @email 1143878025@qq.com
 * @date 2023-02-16 14:12:33
 */
@RestController
@RequestMapping("shop/goods")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;

    @Autowired
    private ItemCatService itemCatService;


    /**
     * 列表
     */
    @GetMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = goodsService.queryPage(params);

        return R.ok().put("page", page);
    }

    //根据父id查询对应下面的子节点
    @GetMapping("/findByParentId/{parentId}")
    public R findByParentId(@PathVariable String parentId){
        List<ItemCatEntity> itemCatEntities = itemCatService.findByParentId(parentId);

        return R.ok().put("categoryList",itemCatEntities);
    }

    // 根据商品id查询组合对象
    @GetMapping("/findById/{id}")
    public R findById(@PathVariable String id){
        Goods goods = goodsService.findById(id);
        return R.ok().put("goods",goods);
    }
    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") String id){
		GoodsEntity goods = goodsService.getById(id);

        return R.ok().put("goods", goods);
    }

    /**
     * 保存商品
     */
    @PostMapping("/save")
    public R save(@RequestBody Goods goods){
		goodsService.insert(goods);

        return R.ok();
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    public R update(@RequestBody GoodsEntity goods){
		goodsService.updateById(goods);

        return R.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
    public R delete(@RequestBody String[] ids){
		goodsService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
